package com.hnas.sys.service.system;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.hnas.core.dao.PageBean;
import com.hnas.core.dto.DTOPagedList;
import com.hnas.core.util.DateUtil;
import com.hnas.core.util.StringUtil;
import com.hnas.sys.core.util.UserUtils;
import com.hnas.sys.dao.LogDAO;
import com.hnas.sys.dao.UserDAO;
import com.hnas.sys.orm.LogInfo;
import com.hnas.sys.orm.User;
import com.hnas.sys.service.system.dto.DTOLog;
import com.hnas.sys.service.system.dto.DTOLogSearch;
import com.hnas.sys.service.system.mapper.LogMapper;

@Component("logService")
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public class LogService implements ILogService {
	private static Log log = LogFactory.getLog(LogService.class);
	@Autowired
	private UserDAO userDAO;
	@Autowired
	private LogDAO logDAO;
	public void saveSysLog(String logInfo, String objectType,
			String operateType, long objectId, String objectNo) {
		// TODO Auto-generated method stub
		LogInfo log = new LogInfo();
		log.setLogInfo(logInfo);
		log.setObjectId(objectId);
		log.setObjectType(objectType);
		log.setObjectId(objectId);
		log.setObjectNo(objectNo);
		log.setOperateType(operateType);
		log.setOperateTime(DateUtil.dateTimeToString(new Date()));
		this.logDAO.persist(log);
	}

	public void saveLog(String userAccount, String logInfo, String objectType,
			String operateType, long objectId, String objectNo) {
		// TODO Auto-generated method stub
		User u = this.userDAO.getUserByAccount(userAccount);		
		LogInfo log = new LogInfo();
		log.setOperator(u);
		log.setLogInfo(logInfo);
		log.setObjectId(objectId);
		log.setObjectType(objectType);
		log.setObjectId(objectId);
		log.setObjectNo(objectNo);
		log.setOperateType(operateType);
		log.setOperateTime(DateUtil.dateTimeToString(new Date()));
		String ip = UserUtils.getIpAddress();
		log.setIp(ip);
		this.logDAO.persist(log);
	}
	public DTOPagedList<DTOLog> searchLog(DTOLogSearch con) {
		// TODO Auto-generated method stub
		PageBean<LogInfo> page = new PageBean<LogInfo>();
		page.setPageNo(con.getPageNo());
		page.setPageSize(con.getPageSize());
		page.setTotalCount(con.getTotalCount());
		StringBuffer sb = new StringBuffer();
		int j = 1;
		List paras = new ArrayList();

		DTOLogSearch sc = con;//.getSearchCon();
		if (sc.getObjectType() !=null) {
			sb.append(" and t.objectType=?" + j++);
			paras.add(sc.getObjectType());
		}
		if (sc.getObjectId() > 0) {
			sb.append(" and t.objectId=?" + j++);
			paras.add(sc.getObjectId());
		}
		if (sc.getOperateTypeArray() != null) {
			sb.append(" and t.operateType in ( ");
			for (String ot:sc.getOperateTypeArray()) {
				sb.append(" ?" + (j++) + ",");
				paras.add(ot);
			}
			sb.delete(sb.length() -1, sb.length() );
			sb.append(" ) ");
		}
		if (!StringUtil.isEmpty(sc.getOperateTime1())) {
			sb.append(" and t.operateTime>=?" + j++);
			paras.add(sc.getOperateTime1().trim());
		}
		if (!StringUtil.isEmpty(sc.getOperateTime2())) {
			sb.append(" and t.operateTime<=?" + j++);
			paras.add(sc.getOperateTime2().trim() + " 23:59:59");
		}
		if (sb.length() > 0) {
			sb.delete(0, 5);
			sb.insert(0, " from LogInfo t where ");
		} else {
			sb.append(" from LogInfo t");
		}
		sb.append(" order by t.operateTime desc ");
		page.setQuery(sb.toString());
		page.setQueryCount("select count(t.id) " + sb);
		this.logDAO.queryByPage(page, paras.toArray());
		DTOPagedList<DTOLog> pageList = new DTOPagedList<DTOLog>();
		pageList.setPageNo(page.getPageNo());
		pageList.setPageSize(page.getPageSize());
		pageList.setTotalCount(page.getTotalCount());
		List<DTOLog> ls = new ArrayList<DTOLog>();
		for (LogInfo or : page.getList()) {
			ls.add(LogMapper.getInstance().mapToValue(or));
		}
		pageList.setList(ls);
		return pageList;
	}
}
